package com.szwistar.common.datastruct;

import java.lang.reflect.Method;

/**
 * 通用日志实现类
 * @author awander
 */
public class LOG {
    private static String TAG = "SZW";
    public static String getTAG() { return TAG; }
    public static void setTAG(String TAG) { LOG.TAG = TAG; }

    protected static enum LEVEL {
    	DEBUG, WARN, ERROR, INFO
    }

    static Method stringFormat = null;
    static {
    	try {
			stringFormat = String.class.getMethod("format", new Class[]{String.class, Object[].class});
		} catch (Exception e) {
		}
    }

    private static void out(String tag, LEVEL level, String format, Object[] params) {
		try {
			format = (String) stringFormat.invoke(null, new Object[]{format, params});

			StackTraceElement ste = new Throwable().getStackTrace()[2];
			String filename = ste.getFileName();
			int line = ste.getLineNumber();

			StringBuilder prefix = new StringBuilder();
			if(TAG != null) { prefix.append(TAG).append(": "); }
			prefix.append(level).append(" ");
			prefix.append(filename).append(":").append(line).append(" ");
			prefix.append(format);

	        System.out.println(prefix.toString());
		} catch (Exception e) {
		}
    }

    public static void debug(String format, Object... params) { out(TAG, LEVEL.DEBUG, format, params); }
    public static void warn(String format, Object... params)  { out(TAG, LEVEL.WARN, format, params); }
    public static void error(String format, Object... params) { out(TAG, LEVEL.ERROR, format, params); }
    public static void info(String format, Object... params)  { out(TAG, LEVEL.INFO, format, params); }

    public static void d(String format, Object... params) { out(TAG, LEVEL.DEBUG, format, params); }
    public static void w(String format, Object... params)  { out(TAG, LEVEL.WARN, format, params); }
    public static void e(String format, Object... params) { out(TAG, LEVEL.ERROR, format, params); }
    public static void i(String format, Object... params)  { out(TAG, LEVEL.INFO, format, params); }


    public static void main(String args[]) {
        LOG.debug("test d=%d", 123);
        LOG.error("test e=%d", 123);
        LOG.warn("test w=%d", 123);
        LOG.info("test i=%d", 123);

        LOG.d("test d=%d", 123);
        LOG.e("test e=%d", 123);
        LOG.w("test w=%d", 123);
        LOG.i("test i=%d", 123);
    }
}
